package com.hy.service.impl;

import com.hy.bean.User;
import com.hy.dao.MessageDao;
import com.hy.dao.UserDao;
import com.hy.service.MessageService;
import com.hy.service.UserService;
import com.hy.utils.FileUtils;
import com.hy.utils.ResponseResult;
import com.hy.utils.ResultUtils;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import javax.xml.transform.Source;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MessageServiceImpl implements MessageService {
    private MessageDao messageDao = new MessageDao();

    @Override
    public void listMessage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        List<Map<String,Object>> mapList = messageDao.selectMessage(userId);
        for (int i = 0; i < mapList.size(); i++) {
            mapList.get(i).put("create_time",mapList.get(i).get("create_time").toString().substring(0,10));
        }
        ResponseResult<List<Map<String,Object>>> result = new ResponseResult<>(200,"数据",mapList,101);
        ResultUtils.result(result,request,response);
    }

    @Override
    public void listUserMessage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        Integer id = Integer.valueOf(request.getParameter("id"));
        List<Map<String,Object>> mapList = messageDao.selectUserMessage(id,userId);
        for (int i = 0; i < mapList.size(); i++) {
            mapList.get(i).put("create_time",mapList.get(i).get("create_time").toString().substring(0,10));
        }
        ResponseResult<List<Map<String,Object>>> result = new ResponseResult<>(200,"数据",mapList,101);
        ResultUtils.result(result,request,response);
    }

    @Override
    public void saveMessage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        Integer id = Integer.valueOf(request.getParameter("id"));
        String content = request.getParameter("content");
        boolean b = false;
        Long aLong = messageDao.selectFollowUser(userId, id);
        if(aLong>0){
            // 使用正则表达式提取img标签的src值
            String pattern = "<img[^>]+src\\s*=\\s*['\"]([^'\"]+)['\"][^>]*>";
            Pattern imgPattern = Pattern.compile(pattern);
            Matcher matcher = imgPattern.matcher(content);

            // 处理匹配到的每个img标签
            while (matcher.find()) {
                String originalSrc = matcher.group(1);
                // 保存图片到服务器的upload文件夹中
                ServletContext servletContext = request.getServletContext();
                String newSrc = FileUtils.saveImage(originalSrc,servletContext);

                // 更新content中的src值为新的路径
                content = content.replace(originalSrc, newSrc);
            }
            b = messageDao.insertMessage(id, userId, content);
            ResponseResult<Boolean> result = null;
            if(b){
                result = new ResponseResult<>(200,"数据",b,101);
            }else {
                result = new ResponseResult<>(500,"提交错误",b,102);
            }
            ResultUtils.result(result,request,response);
        }else {
            List<Map<String, Object>> mapList = messageDao.selectFollowUserMessageCount(userId, id);
            ResponseResult<Boolean> result = null;
            if(userId.toString().equals(mapList.get(0).get("user_id").toString()))
                result = new ResponseResult<>(500, "未互相关注前，不能连续发两条", b, 102);
            else {
                b = messageDao.insertMessage(id, userId, content);
                result = new ResponseResult<>(200,"数据",b,101);
            }
            ResultUtils.result(result,request,response);
        }


    }

    @Override
    public void listMessageList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        String username = request.getParameter("username");
        List<Map<String,Object>> mapList = messageDao.selectMessageList(userId,username);
        for (int i = 0; i < mapList.size(); i++) {
            mapList.get(i).put("create_time",mapList.get(i).get("create_time").toString().substring(0,10));
        }
        ResponseResult<List<Map<String,Object>>> result = new ResponseResult<>(200,"数据",mapList,101);
        ResultUtils.result(result,request,response);
    }

    @Override
    public void listFollowUserMessage(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Integer userId = Integer.valueOf(request.getParameter("userId"));
        List<Map<String,Object>> mapList = messageDao.selectFollowUserMessage(userId);
        ResponseResult<List<Map<String,Object>>> result = new ResponseResult<>(200,"数据",mapList,101);
        ResultUtils.result(result,request,response);
    }

}
